package com.yida.wy.basic.controller;

import cn.hutool.core.bean.BeanUtil;
import com.mybatisflex.core.paginate.Page;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.file.MimeTypeUtils;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.system.api.domain.SysFile;
import com.yida.wy.basic.domain.WyBasePartyOrganization;
import com.yida.wy.basic.domain.request.WyBasePartyOrganizationRequest;
import com.yida.wy.basic.service.IWyBasePartyOrganizationService;
import com.yida.wy.common.service.IWyFileService;
import com.yida.wy.common.utils.SnowFlakeUtil;
import com.yida.wy.common.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;

/**
 * 党组织基础信息 控制层。
 *
 * @author lqq
 * @since 1.0
 */
@RestController
@RequestMapping("/wyBasePartyOrganization")
@Api(tags = "党组织基础信息")
public class WyBasePartyOrganizationController extends BaseController {

    @Resource
    private IWyBasePartyOrganizationService wyBasePartyOrganizationService;

    @Resource
    private IWyFileService wyFileService;

    /**
     * 党组织架构图上传
     *
     * @param file 党组织架构图
     * @return /
     */
    @Log(title = "党组织架构图上传", businessType = BusinessType.UPDATE, systemName = "wy")
    @PostMapping(value = "/upload", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "党组织架构图上传", response = SysFile.class)
    @RequiresPermissions("wy:basis:PartyOrganization:upload")
    public AjaxResult architectureUpload(@RequestPart("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return AjaxResult.error("上传图片失败,图片异常");
        }
        String extension = FileTypeUtils.getExtension(file);
        if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
            return AjaxResult.error("上传图片失败,图片格式不正确" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
        }
        SysFile sysFile = wyFileService.uploadFileDynamicLink(file, null, true);
        return AjaxResult.success("上传图片成功", sysFile);
    }

    /**
     * 添加 党组织基础信息
     *
     * @param request 党组织基础信息
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping
    @RequiresPermissions("wy:basis:PartyOrganization:add")
    @ApiOperation(value = "添加党组织基础信息", notes = "添加党组织基础信息")
    @Log(title = "党组织基础信息-新增", businessType = BusinessType.INSERT, systemName = "wy")
    public AjaxResult save(@Validated @RequestBody WyBasePartyOrganizationRequest request) {
        request.setPartyOrganizationId(SnowFlakeUtil.getWyNextId());
        return toAjax(wyBasePartyOrganizationService.save(BeanUtil.copyProperties(request, WyBasePartyOrganization.class)));
    }

    /**
     * 根据主键删除党组织基础信息
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据主键删除党组织基础信息", notes = "根据主键删除党组织基础信息")
    @RequiresPermissions("wy:basis:PartyOrganization:remove")
    @Log(title = "党组织基础信息-删除", businessType = BusinessType.DELETE, systemName = "wy")
    public AjaxResult remove(@PathVariable Serializable id) {
        return toAjax(wyBasePartyOrganizationService.removeById(id));
    }

    /**
     * 根据主键更新党组织基础信息
     *
     * @param request 党组织基础信息
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping
    @RequiresPermissions("wy:basis:PartyOrganization:edit")
    @ApiOperation(value = "根据主键更新党组织基础信息", notes = "根据主键更新党组织基础信息")
    @Log(title = "党组织基础信息-更新", businessType = BusinessType.UPDATE, systemName = "wy")
    public AjaxResult update(@Validated @RequestBody WyBasePartyOrganizationRequest request) {
        return toAjax(wyBasePartyOrganizationService.updateById(BeanUtil.copyProperties(request, WyBasePartyOrganization.class)));
    }

    /**
     * 根据党组织基础信息主键获取详细信息。
     *
     * @param id wyBasePartyOrganization主键
     * @return 党组织基础信息详情
     */
    @GetMapping("/getInfo/{id}")
    @RequiresPermissions("wy:basis:PartyOrganization:query")
    @ApiOperation(value = "根据党组织基础信息主键获取详细信息", notes = "根据党组织基础信息主键获取详细信息")
    public AjaxResult getInfo(@PathVariable Serializable id) {
        return AjaxResult.success(wyBasePartyOrganizationService.getById(id));
    }

    /**
     * 分页查询党组织基础信息
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/list")
    @RequiresPermissions("wy:basis:PartyOrganization:list")
    @ApiOperation(value = "分页查询党组织基础信息", notes = "分页查询党组织基础信息")
    public TableDataInfo page(Page<WyBasePartyOrganization> page) {
        return getDataTable(wyBasePartyOrganizationService.page(page));
    }
}